﻿using log4net;
using System;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

namespace Commonality.Model
{
    public class Log4Net
    {
        /// <summary>
        /// 写日记,此处存储日志于两个文件中，据情况可增加文件
        /// </summary>
        private static ILog SystemLogger = log4net.LogManager.GetLogger("SystemLog");  //存储日志于System.log文件中，主日志文件
        private static ILog TcpLogger = log4net.LogManager.GetLogger("TcpLog");      //存储日志于TcpLog.log文件中，通讯内容的日志文件
        private static ILog NgLogger = log4net.LogManager.GetLogger("NgLog");

        public Log4Net()
        {

        }

        //不打印相同内容的Log
        private static string strTempMessage = "";
        /// <summary>
        /// 同时写日记及主页的运行状态
        /// </summary>
        /// <param name="message"></param>
        public static void MsgInfo(object message)
        {
            if (strTempMessage != message.ToString())
            {
                strTempMessage = message.ToString();
                SystemLogger.Info(message);
                LogPrint.ShowInfo(message, "");
            }
        }
        public static void MsgTcpInfo(object message)
        {
            if (strTempMessage != message.ToString())
            {
                strTempMessage = message.ToString();
                TcpLogger.Info(message);
                SystemLogger.Info(message);
                LogPrint.ShowInfo(message, "");
            }
        }
        public static void MsgNgInfo(object message)
        {
            if (strTempMessage != message.ToString())
            {
                strTempMessage = message.ToString();
                NgLogger.Info(message);
                SystemLogger.Info(message);
                LogPrint.ShowInfo(message, "");
            }
        }
        public static void MsgDebug(object message)
        {
            if (strTempMessage != message.ToString())
            {
                strTempMessage = message.ToString();
                SystemLogger.Debug(message);
                //LogPrint.Info5(message.ToString(), 1);
            }
        }
        public static void MsgTcpDebug(object message)
        {
            if (strTempMessage != message.ToString())
            {
                strTempMessage = message.ToString();
                TcpLogger.Debug(message);
                SystemLogger.Debug(message);
                //LogPrint.Info5(message.ToString(), 1);
            }
        }

        public static void MsgWarn(object message)
        {
            if (strTempMessage != message.ToString())
            {
                strTempMessage = message.ToString();
                SystemLogger.Warn(message);
                LogPrint.ShowInfo(message, "  [WARN]");
            }
        }
        public static void MsgTcpWarn(object message)
        {
            if (strTempMessage != message.ToString())
            {
                strTempMessage = message.ToString();
                TcpLogger.Warn(message);
                SystemLogger.Warn(message);
                LogPrint.ShowInfo(message, "  [WARN]");
            }
        }

        public static void MsgError(object message)
        {
            if (strTempMessage != message.ToString())
            {
                strTempMessage = message.ToString();
                SystemLogger.Error(message);
                LogPrint.ShowInfo(message, "  [ERROR]");
            }
        }
        public static void MsgTcpError(object message)
        {
            if (strTempMessage != message.ToString())
            {
                strTempMessage = message.ToString();
                TcpLogger.Error(message);
                SystemLogger.Error(message);
                LogPrint.ShowInfo(message, "  [ERROR]");
            }
        }

        #region 写日记方法
        public void Debug(object message)
        {
            SystemLogger.Debug(message);
        }
        public void Debug(object message, Exception exception)
        {
            SystemLogger.Debug(message, exception);
        }

        public void Info(object message)
        {
            SystemLogger.Info(message);
        }
        public void Info(object message, Exception exception)
        {
            SystemLogger.Info(message, exception);
        }

        public void Warn(object message)
        {
            SystemLogger.Warn(message);
        }
        public void Warn(object message, Exception exception)
        {
            SystemLogger.Warn(message, exception);
        }

        public void Error(object message)
        {
            SystemLogger.Error(message);
        }
        public void Error(object message, Exception exception)
        {
            SystemLogger.Error(message, exception);
        }

        public void Fatal(object message)
        {
            SystemLogger.Fatal(message);
        }
        public void Fatal(object message, Exception exception)
        {
            SystemLogger.Fatal(message, exception);
        }
        #endregion
    }

    public class LogPrint
    {
        public delegate void BoilerLogHandler(object Info, string InfoLevel);
        public static event BoilerLogHandler BoilerEventLog;
        public static void ShowInfo(object info, string infoLevel)
        {
            if (info != null)
            {
                BoilerEventLog(info, infoLevel);
            }
        }
    }
}
